#################
#    Imports    #
#################
import random
import sys
import os
import time
from checkinput import checkInput
##################################
#        Global Variables        #
##################################
playerName = ""
playerClass = ""
playerHealth = 100
enemyHealth = 100
previousLocal = ""
playerGold = 0
enemyEquippedWeapon = ""
equippedWeapon = ""

weapons = {
    
    "None": {"damage": 0, "value": 0, "weight": 0, "tier": 0, "weaponName": "None",},
    'Wooden Staff': {"damage": lambda: random.uniform(1, 7), "value": 1, "weight": 5, "teir": 0, "weaponName": "Wooden Staff"},
    "Wooden Club": {"damge": lambda: random.uniform(1, 6), "value": 1, "weight": 3, "tier": 0, "weaponName": "Wooden Club", },

    "Iron Dagger": {"damage": lambda: random.uniform(2, 10), "value": 5, "weight": 2, "tier": 1, "weaponName": "Iron Dagger"},
    "Iron Sword": {"damage": lambda:  random.uniform(5, 13), "value": 7, "weight": 9,"tier": 1, "weaponName": "Iron Sword"},

    "Steel Dagger": {"damage": lambda:  random.uniform(6, 13), "value": 10, "weight": 4, "tier": 2, "weaponName": "Steel Dagger"},
    "Steel Sword": {"damage": lambda:  random.uniform(7, 15), "value": 15, "weight": 18, "tier": 2, "weaponName": "Steel Sword"},

    "Titanium Dagger": {"damage": lambda:  random.uniform(15, 25), "value": 20, "weight": 8, "tier": 3, "weaponName": "Titanium Dagger"},
    "Titanium Sword": {"damage": lambda:  random.uniform(20, 30),  "value": 25, "weight": 27, "tier": 3, "weaponName": "Titanium Sword"}

}

############################
#     Player Inventory     #
############################
def inventory():
    weight = 0
    maxWeight = 0
    global playerClass
    if playerClass.lower() == "sorcerer":
        maxWeight = 50
    elif playerClass.lower() == "assassin":
        maxWeight = 75
    elif playerClass.lower() == "warrior":
        maxWeight = 100
    if weight == maxWeight:
        print("You are already carrying " + maxWeight + "! You cannot carry\
any more!")
        items ={}

    
   
def goBack():
    if previousLocal == "part1":
        part1()
    elif previousLocal == "part1Encounter1":
        part1Encounter1()
    elif previousLocal == "part2":
        part2()

def clear():
    os.system('cls')

##############################
#            Game            #
##############################

def main():
    print ("Would you like to start a new journey, return to your previous point,\
or leave?  (please type new, load, or exit)")
    gameChoice = input()
    if gameChoice == "new":
        newGame()
    elif gameChoice == "load":
        loadGame()
    elif gameChoice == "exit":
        quit()
    else:
        clear()
        main()

def newGame():
    global playerClass
    global playerName
    while playerName == "":
        print ("What is your name?")
        playerName = input()
        
    while checkInput(playerClass, ['sorcerer', 'warrior', 'assassin']) == False: 
        print ("Are you a sorcerer, a warrior, or an assassin? \
(type sorcerer, warrior, or assassin)")
        playerClass = input()
        if playerClass.lower() == "sorcerer":
            equippedWeapon = weapons["None"]
        elif playerClass.lower() == "assassin":
            equippedWeapon = weapons["Iron Dagger"]
        elif playerClass.lower() == "warrior":
            equippedWeapon = weapons["Iron Sword"]
    else:
            clear()
            part1()    

def part1():
    global previousLocal
    previousLocal = part1
    print ("Walking into a dark forest, you see a path going left, \
and a path going straight. Which path would you like to take?")
    path1 = input()
    if path1 == "straight":
        part1Encounter1()
    elif path1 == "left":
        print ("Turning left, you trip over a log. Falling down, \
you hit your head on a rock and start bleeding")
        global playerHealth
        playerHealth = playerHealth - 2
        print ()
        print ("After getting up and tying a piece of cloth around your wound, \
you continue onward")
        input()


def part1Encounter1():
    global enemyEquippedWeapon
    enemyEquippedWeapon = weapons["Wooden Staff"]
    global previousLocal
    previousLocal = part1Encounter1
    print ("Continuing onward, you encounter a lone traveller")
    print ("Do you approach him?[Y/N]")
    encounter1 = input()
    if encounter1.lower() == 'y':
        global playerClass
        if playerClass.lower() == 'sorcerer':
            print ("As you approach, the traveller lunges at you with a staff")
            magicCombat()
        elif playerClass.lower() == 'warrior':
            print ("As you approach, the traveller lunges at you with a staff")
            meleeCombat()
        elif playerClass.lower() == 'assassin':
            stealthChance = random.randint(1, 100)
            if stealthChance <= 10 or stealthChance >= 90:
                print ("You approach the traveller unnoticed")
                stealthCombatSneak()
            else:
                print ("The traveller heard your footsteps, and lunges at you with a \
staff")
            stealthCombatNoticed()
    elif encounter1.lower() == 'n':
        clear()
        print ("Ignoring the traveller, you trek onwards")
        print (enemyEquippedWeapon)
    else:
        clear()
        part1Encounter1()



##############
#   Combat   #
##############

def magicCombat():
    global enemyEquippedWeapon
    global enemyHealth
    global playerHealth
    elements = ['fire', 'ice', 'electricity']
    while enemyHealth > 0:
        print ("You blast "+random.choice(elements)+" from your hands, damaging the\
 enemy")
        input()
        enemyHealth -= random.randint(15, 30)
        clear()
        print ("The enemy swipes at you with his weapon")
        playerDodgeChance = random.randint(1, 10)
        if playerDodgeChance <= 2:
            print()
            print ("You jump back, dodging his strike")
        else:
            print()
            print ("You were too slow! His blow lands on you")
            playerHealth -= weapons[enemyEquippedWeapon]["damage"]
    else:
        if enemyHealth <= 0:
            print ("You killed your opponent. Do you wish to loot him? [Y/N]")
            loot = input()
            if loot.lower == 'y':
                loot()
            else:
                goBack()

def stealthCombatSneak():
    global enemyEquippedWeapon
    global enemyHealth
    global playerHealth
    critChance = random.randint(1, 100)
    attack = 1
    while enemyHealth != 0 and attack == 1:
        print ("You sneak up behind your opponent, and lunge at him with \
your "+ weapons[equippedWeapon]["weaponName"])
        playerHealth -= weapons[enemyEquippedWeapon]["damage"]*10
        attack += 1
    else:
        if enemyHealth != 0 and attack > 1 and critChance >= 98:
            print("You slash at your opponent, landing a lucky blow")
            enemyHealth -= weapons[equippedWeapon]["damage"]*3
            attack +=1
            input()
        elif enemyHealth != 0 and attack > 1 and critChance < 98:
            print("You slash at your opponent")
            enemyHealth -= weapons[equippedWeapon](["damage"])
            attack +=1
            input()
        elif enemyHealth <= 0:
            print ("You killed your opponent. Do you wish to loot him?")
            loot = input('y/n')
            if loot.lower == 'y':
                loot()
            else:
                goBack()

def stealthCombatNoticed():
    global enemyEquippedWeapon
    global enemyHealth
    global playerHealth
    critChance = random.randint(1, 100)
    while enemyHealth > 0:
        if critChance >= 98:
            print("You slash at your opponent, landing a lucky blow")
            enemyHealth -= weapons[equippedWeapon]["damage"]*3
            input()
        elif critChance < 98:
            print("You slash at your opponent")
            enemyHealth -= weapons[equippedWeapon]["damage"]
            input()
        elif enemyHealth <= 0:
            print ("You killed your opponent. Do you wish to loot him?")
            loot = input('y/n')
            if loot.lower == 'y':
                loot()
            else:
                goBack()

def meleeCombat(self):
    global enemyEquippedWeapon
    global enemyHealth
    global playerHealth
    pass

##############
#    Loot    #
##############
def loot():
    global playerGold
    foundGold = random.randint(0, 10)
    print ("You find ", foundGold, " on the body")
    playerGold += foundGold
    foundWeapon = random.choice(weapons)
    print ("You find ", foundWeapon, " on the body. \
Would you like to take it? [y/n]")
    choice = input()
    if choice.lower() == "y":
        items = foundWeapon
        weight += foundweapon["weight"]
    


main()
